//
//  WeatherViewController.m
//  EDPS
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 张维扬. All rights reserved.
//

#import "WeatherViewController.h"
#import "ZWY_NetHandle.h"
#import "AddresssViewController.h"
#import "WeatherCollectionViewCell.h"
#import "UIImage+BlurAlpe.h"

#define HOLS [[UIScreen mainScreen] bounds]

@interface WeatherViewController ()<AddresssViewControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,assign) BOOL isOpen;
@property (nonatomic,retain) UICollectionView *collectionView;
@property (nonatomic,retain) UICollectionViewFlowLayout *layout;
@property (nonatomic,retain) NSString *Locationcity;
@property (nonatomic,retain) NSMutableArray *tempArr;
@property (nonatomic,assign) int hour;
@property (nonatomic,retain) UITableView *tableView;

@end

@implementation WeatherViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftAct)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"城市" style:UIBarButtonItemStyleDone target:self action:@selector(changeCity)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
    self.title = @"天气情况";
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    UIImage *image = [UIImage imageNamed:@"citty_1024.jpg"];
    imageView.image = [image boxblurImageWithBlur:0.1];
    [self.view addSubview:imageView];
    
    self.Locationcity = @"大连";
    [self creatTableView];
    [self creatViews];
    [self creatData];
}
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, HOLS.size.width, 300) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"des"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"des"];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, HOLS.size.width, 300)];
    view.backgroundColor = [UIColor clearColor];
    UILabel *cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 120, HOLS.size.width, 60)];
    NSString *cityTe = [self.Locationcity stringByAppendingString:@"市"];
    cityLabel.text = cityTe;
    cityLabel.textAlignment = 1;
    [cityLabel setFont:[UIFont systemFontOfSize:26]];
    [view addSubview:cityLabel];
    NSMutableDictionary *dictionary = [self.tempArr objectAtIndex:0];
    NSString *temp1 = [dictionary objectForKey:@"temp1"];
    NSString *tomp1 = [temp1 stringByAppendingString:@"℃"];
    NSString *temp2 = [dictionary objectForKey:@"temp2"];
    NSString *tomp2 = [temp2 stringByAppendingString:@"℃"];
    NSInteger temp1Int = temp1.intValue;
    NSInteger temp2Int = temp2.intValue;
    NSString *temp = [NSString string];
    if (temp1Int < temp2Int)
    {
        temp = [tomp1 stringByAppendingFormat:@"~%@",tomp2];
    }
    else
    {
        temp = [tomp2 stringByAppendingFormat:@"~%@",tomp1];
    }
    UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 220, HOLS.size.width, 45)];
    tempLabel.text = temp;
    tempLabel.textAlignment = 1;
    [tempLabel setFont:[UIFont systemFontOfSize:24]];
    [view addSubview:tempLabel];
    
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 300 ;
}
- (void)creatViews
{
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    self.layout.itemSize = CGSizeMake(HOLS.size.width / 3 ,self.view.frame.size.width / 5 * 2);
    self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.layout.sectionInset = UIEdgeInsetsMake(0 ,0,0,0 );
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 300, HOLS.size.width, HOLS.size.width / 4 * 2) collectionViewLayout:self.layout];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[WeatherCollectionViewCell class] forCellWithReuseIdentifier:@"itm"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.tempArr.count != 0)
    {
        return self.tempArr.count;
    }
    else
    {
        return 0;
    }
}
//cell内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WeatherCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"itm" forIndexPath:indexPath];
    cell.layer.borderColor = [UIColor lightGrayColor].CGColor ;
    cell.layer.borderWidth = 0.3;
    cell.backgroundColor = [UIColor clearColor];
    if (self.tempArr.count != 0)
    {
        NSMutableDictionary *dic = [self.tempArr objectAtIndex:indexPath.row];
        cell.dic = dic;
    }
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (void)creatData
{
    NSString *city = self.Locationcity;
    NSString * cityStr = [city stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
    NSString *URL = [NSString stringWithFormat:@"http://v.juhe.cn/weather/forecast3h.php?cityname=%@&key=96390dd4b3620ccf089a3728842cda63",cityStr];
    NSLog(@"URL = %@ ",URL);
    [ZWY_NetHandle getURL:URL Cookie:nil parameter:nil result:^(id result) {
        NSMutableDictionary *dic = result;
        self.tempArr = [dic objectForKey:@"result"];
        [self.collectionView reloadData];
        [self.tableView reloadData];
    }];
}

- (void)leftAct
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)changeCity
{
    AddresssViewController *address = [[AddresssViewController alloc] init];
    address.delegate = self;
    [self.navigationController pushViewController:address animated:YES];
    
}
- (void)passDelegate:(NSString *)city
{
    NSRange a = {0,city.length - 1};
    NSString *CurrentCity = [city substringWithRange:a];
    self.Locationcity = CurrentCity;
    [self creatData];
}

@end